輸入一棵二叉樹,求該樹的深度。從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度為樹的深度。


//遞歸方式
    public int TreeDepth(TreeNode root) {
        if(root == null){
        return 0;
    }
        TreeNode node = root;
        int left = TreeDepth(node.left);
        int right = TreeDepth(node.right);
        int res = left=left>right?left:right;
        return res+1;

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM