1. 程式人生 > 其它 >Leetcode226/101/104/111之二叉樹中的遞迴例題

Leetcode226/101/104/111之二叉樹中的遞迴例題

二叉樹中的遞迴例題

Leetcode226-翻轉二叉樹

  • 給你一棵二叉樹的根節點 root ,翻轉這棵二叉樹,並返回其根節點
  • 輸入:root = [4,2,7,1,3,6,9]
  • 輸出:[4,7,2,9,6,3,1]
//遞迴先序遍歷的變形
public class L226 {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        swap(root);
        invertTree(root.left);
        invertTree(root.right);

        return root;

    }

    public void swap(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}

Leetcode101-對稱二叉樹

  • 給你一個二叉樹的根節點 root , 檢查它是否軸對稱
  • 輸入:root = [1,2,2,3,4,4,3]
  • 輸出:true
//遞迴後序遍歷變形
//本題遍歷只能是“後序遍歷”,因為我們要通過遞迴函式的返回值來判斷兩個子樹的內側節點和外側節點是否相等
public class L101 {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }else{
            return isDuiChen(root.left,root.right);
        }
    }

    public boolean isDuiChen(TreeNode leftNode,TreeNode rightNode){
        if(leftNode==null && rightNode==null){
            return true;
        }
        if(leftNode==null || rightNode== null){
            return false;
        }
        if(leftNode.val!=rightNode.val){
            return false;
        }
        boolean outDuiChen=isDuiChen(leftNode.left,rightNode.right);
        boolean inDuiChen=isDuiChen(leftNode.right,rightNode.left);
        return outDuiChen&&inDuiChen;
    }
}

Leetcode104-二叉樹的最大深度

  • 給定一個二叉樹,找出其最大深度。
  • 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。
  • 給定二叉樹 [3,9,20,null,null,15,7]
  • 返回它的最大深度 3
public class L104 {
    //遞迴法後序遍歷
    public int maxDepth1(TreeNode root) {
        return getDepth(root);
    }

    public int getDepth(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftDepth=getDepth(root.left);
        int rightDepth=getDepth(root.right);
        int finalDepth=Math.max(leftDepth,rightDepth)+1;
        return finalDepth;
    }

    //遞迴法前序遍歷
    int finalRes=1;
    public int maxDepth2(TreeNode root) {
        if(root==null){
            return 0;
        }
        getDepth2(root,1);
        return finalRes;
    }

    public void getDepth2(TreeNode root,int depth){
        if(finalRes<depth){
            finalRes=depth;
        }
        if(root.left==null && root.right==null){
            return;
        }
        if(root.left!=null){
            depth++;
            getDepth2(root.left,depth);
            depth--;
        }
        if(root.right!=null){
            depth++;
            getDepth2(root.right,depth);
            depth--;
        }
    }


    //迭代法
    public int maxDepth3(TreeNode root) {
        if (root==null)
            return 0;
        else {
            return order(root);
        }
    }


    public int order(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int max=0;
        while (!queue.isEmpty()) {
            max++;
            int currentLevelSize = queue.size();
            for (int i = 0; i < currentLevelSize; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }
        return max;
    }
}

Leetcode111-二叉樹的最小深度

  • 給定一個二叉樹,找出其最小深度。
  • 輸入:root = [3,9,20,null,null,15,7]
  • 輸出:2
//遞迴的變形
public class L111 {
    public int minDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        int leftDepth=minDepth(root.left);
        int rightDepth=minDepth(root.right);
        //如果左子樹為空,右子樹不為空,說明最小深度是 1 + 右子樹的深度
        if (root.left == null) {
            return rightDepth + 1;
        }
        //同上
        if (root.right == null) {
            return leftDepth + 1;
        }
        return Math.min(leftDepth,rightDepth)+1;
    }
}