Java O(n) Postorder Traversal


  • 4
    public class Solution {
        
        int tilt = 0;
        
        public int findTilt(TreeNode root) {
            postorder(root);
            return tilt;
        }
        
        public int postorder(TreeNode root) {
            if (root == null) return 0;
            int leftSum = postorder(root.left);
            int rightSum = postorder(root.right);
            tilt += Math.abs(leftSum - rightSum);
            return leftSum + rightSum + root.val;
        }
        
    }
    

  • 0
    G
    This post is deleted!

  • 0
    B

    How is the big O calculated for this one?


Log in to reply
 

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