C#, Simple Solution, Postorder Traversal


  • 0
    D
    public class Solution {
        private int m_diameter = 0;
        public int DiameterOfBinaryTree(TreeNode root) {
            PostOrderTraversal(root);
            return m_diameter;
        }
        private int PostOrderTraversal(TreeNode root) {
            if(root == null)
            {
                return 0;
            }
            int left = PostOrderTraversal(root.left);
            int right = PostOrderTraversal(root.right);
            m_diameter = Math.Max(m_diameter, left+right);
            return 1 + Math.Max(left, right);
        }
    }
    

Log in to reply
 

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