C# recursive


  • 0
    H
    public int DiameterOfBinaryTree(TreeNode root) {
            int d = 0;
            Recurse(root, ref d);
            
            return d;
        }
        
        public int Recurse(TreeNode node, ref int max) {
            if (node == null) return 0;
            int left = Recurse(node.left, ref max);
            int right = Recurse(node.right, ref max);
            
            max = Math.Max(max, left + right);
            return Math.Max(left, right) + 1;
        }
    

Log in to reply
 

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