Inorder traversal kind of solution


  • 0
    M
    public int sumOfLeftLeaves(TreeNode root) {
            if (root == null) return 0;
            int sum = 0;
            Stack<TreeNode> st = new Stack<TreeNode>();
            TreeNode curr = root;
            while (curr != null || !st.empty()) {
                while (curr != null) {
                    st.push(curr);
                    curr = curr.left;
                }
                curr = st.pop();
                if (curr.right == null && curr.left == null) {
                    if (!st.empty() && curr == st.peek().left) {
                        sum += curr.val;
                    }
                }
                curr = curr.right;
            }
            return sum;
        }
    

Log in to reply
 

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