using the DFS un-recursion version


  • 0
    W

    class Solution {
    public int sumNumbers(TreeNode root) {
    Stack<TreeNode> s1 =new Stack<>();
    if(root==null) return 0;
    s1.push(root);
    int sum=0;
    while(!s1.isEmpty()){
    TreeNode cur = s1.pop();
    if(cur.left==null&&cur.right==null)
    sum+=cur.val;
    if(cur.left!=null){
    cur.left.val+=cur.val10;
    s1.push(cur.left);
    }
    if(cur.right!=null){
    cur.right.val+=cur.val
    10;
    s1.push(cur.right);
    }
    }
    return sum;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.