LeetCode刷題之樹

  • Invert Binary Tree-Number226

解題思路:

    交換左右子樹,將左子樹和右子樹看作“數”,那麼該問題就跟交換兩個數類似,只不過需要採用遞歸的方法來進行交換(非遞歸也可以)

代碼:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null)
            return null;
        TreeNode temp = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(temp);
        return root;
        
    }
}
  •  Maximum Depth of Binary Tree -Number 104

思路:

    該題也是遞歸的典型應用,求二叉樹的最大深度,只需要找出左右子樹的最大深度,然後選取左右子樹的較大值+1(root節點的高度),遞歸計算左右子樹的高度。

代碼:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        return root ==null ?0:(1+Math.max(maxDepth(root.left),maxDepth(root.right)));
    }
}
  •     111 Minimum Depth of Binary Tree-E

思路:

    使用深度優先搜索完成,利用遞歸。分爲四種情況:

 1)若當前節點不存在,直接返回0

 2)若當前節點的左子節點不存在,那麼對右子節點調用遞歸函數,並+1返回

 3)若當前節點的右子節點不存在,那麼對左子節點調用遞歸函數,並+1返回

 4)若左右子節點都存在,則分別對左右子樹節點調用遞歸函數,並將兩者種較小值+1返回。

代碼:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)
            return 0;
        if(root.left==null)
            return (1 +minDepth(root.right));
        if(root.right == null)
            return (1+minDepth(root.left));
        return 1+Math.min(minDepth(root.left),minDepth(root.right));
    }
}
  • Validate Binary Tree-Number 98

思路:

    判斷一顆二叉樹是否爲排序二叉樹,我們可以利用定義來解決,即根的值>左子樹,<右子樹。

 也可以利用中序遍歷來做,排序二叉樹的中序遍歷的結果是一個有序的數

代碼:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root ==null)
            return true;
        return valid(root,Long.MIN_VALUE, Long.MAX_VALUE);
    }
    public boolean valid(TreeNode root, long low, long high){
        if(root ==null)
            return true;
        if(root.val<=low || root.val>=high)
            return false;
        return valid(root.left,low,root.val)&&valid(root.right,root.val,high);
    }
}
  • Paths Sum-Number

思路:

    利用深度優先算法來遍歷一條從節點到葉節點的路徑,利用遞歸不停的找節點的左右子節點。存在三種情況:

    1):輸入是一個空節點,那麼直接返回false

    2):只有一個根節點,判斷根節點跟sum的值,如果相等返回true,不等返回false

    3):遞歸:可以同時兩個方向一個遞歸,利用||連接,只要一個是True,返回就爲true。遞歸調用左右子樹,此時的sum值應該爲原sum-當前節點的值。

代碼:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null)
            return false;
        if(root.left==null&&root.right==null &&root.val ==sum)
            return true;
        return hasPathSum(root.left,sum-root.val) || hasPathSum(root.right,sum-root.val);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章