129. Sum Root to Leaf Numbers

题目129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/
2 3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.

1,深搜
思路:根据二叉树定义,深度搜索
public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }
    
    private int dfs(TreeNode root,int curSum){
        if(root == null){
            return 0;
        }
        
        curSum = curSum * 10 + root.val;
        if(root.left == null && root.right == null){
            return curSum;
        }
        
        return dfs(root.left,curSum) + dfs(root.right,curSum); 
    }
2,利用层次遍历
思路:每个节点保存根节点到当前节点的值,例如路径1->2->3 ,节点1是根节点,保存值1,节点2保
存值12,节点3保存值123,遇到叶子节点将其结果叠加到totalSum
public int sumNumbers(TreeNode root) {
        if(root == null){
            return 0;
        }
        
        int totalSum = 0;
        List curLevelNodes = new ArrayList();
        curLevelNodes.add(root);
        while(!curLevelNodes.isEmpty()){
            List nextLevelNodes = new ArrayList();
            for(TreeNode node : curLevelNodes){
                //叶子节点
                if(node.left == null && node.right == null){
                    totalSum += node.val;
                }
                
                if(node.left != null){
                    node.left.val += node.val * 10;
                    nextLevelNodes.add(node.left);
                }
                
                if(node.right != null){
                    node.right.val += node.val * 10;
                    nextLevelNodes.add(node.right);
                }
            }
            curLevelNodes = nextLevelNodes;
        }
        return totalSum;
     }
3,利用后序遍历
思路:利用后序遍历中栈中保存的是根节点到当前节点的路径
public int sumNumbers(TreeNode root) {
        int totalSum = 0;
        Stack stack = new Stack();
        TreeNode node = root;
        boolean hasVisited = true;
        do{
            while(node != null){
                stack.push(node);
                node = node.left;
            }
            
            TreeNode tempNode = null;
            hasVisited = true;
            while(!stack.empty() && hasVisited){
                node = stack.peek();
                if(node.right == tempNode){
                    if(node.left == null && node.right == null){
                        int sum = 0;
                        for(TreeNode tn : stack){
                            sum = (sum * 10 + tn.val);
                        }
                        totalSum += sum;
                    }
                    tempNode = stack.pop();
                }else{
                    hasVisited = false;
                    node = node.right;
                }
            }
        }while(!stack.empty());
        return totalSum;
     }

你可能感兴趣的:(129. Sum Root to Leaf Numbers)