ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

226.翻转二叉树

2021-10-05 18:32:47  阅读:142  来源: 互联网

标签:right TreeNode cur 遍历 二叉树 226 left root 翻转


翻转一棵二叉树。

示例:

 

 

针对二叉树的问题,解题之前一定要想清楚究竟是前中后序遍历,还是层序遍历

这道题目使用前序遍历和后序遍历都可以,唯独中序遍历不方便,因为中序遍历会把某些节点的左右孩子翻转了两次!建议拿纸画一画,就理解了

那么层序遍历可以不可以呢?依然可以的!只要把每一个节点的左右孩子翻转一下的遍历方式都是可以的!

 

1、前序、后序遍历递归实现

 public TreeNode invertTree(TreeNode root) {
         postInvert(root);
         return root;

    }

    private void postInvert(TreeNode root){
        if(root==null){
            return;
        }
         //swap(root);  
        postInvert(root.left);
        postInvert(root.right);
        swap(root);
       
    }

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

  

  2、层序遍历

public TreeNode invertTree(TreeNode root) {
         levelOrder(root);
         return root;

    }

    private void levelOrder(TreeNode root){
        if(root==null){
            return;
        }

        TreeNode cur=root;
        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.add(cur);
        while(!queue.isEmpty()){// cur l r .l r.l r
            cur=queue.pop();
            //System.out.println(cur.val);
            swap(cur);
            if(cur.left!=null){
                queue.add(cur.left);
            }
            if(cur.right!=null){
                queue.add(cur.right);
            }
        }
         
       
    }

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

  

3、前序遍历非递归实现

  public TreeNode invertTree(TreeNode root) {
         preOrder(root);
         return root;

    }

    private void preOrder(TreeNode root){
        if(root==null){
            return;
        }

        //cur l r. stack  cur r l
        TreeNode cur=root;
        Stack<TreeNode> stack=new Stack<>();
        stack.push(cur);
        while(!stack.isEmpty()){
            cur=stack.pop();
            swap(cur);
            if(cur.right!=null){
                stack.push(cur.right);
            }
            if(cur.left!=null){
                stack.push(cur.left);
            }
        }

         
       
    }

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

  4、后序遍历非递归实现

public TreeNode invertTree(TreeNode root) {
         postOrder(root);
         return root;

    }

    private void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        //l r cur. stack1 cur l r. stack cur r l
        TreeNode cur=root;
        Stack<TreeNode> stack1=new Stack<>();
        Stack<TreeNode> stack2=new Stack<>();
        stack1.push(cur);
        while(!stack1.isEmpty()){
            cur=stack1.pop();
            stack2.push(cur);
            if(cur.left!=null){
                stack1.push(cur.left);
            }
            if(cur.right!=null){
                stack1.push(cur.right);
            }
        }
        while(!stack2.isEmpty()){
            swap(stack2.pop());
        }

    }

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

  

标签:right,TreeNode,cur,遍历,二叉树,226,left,root,翻转
来源: https://www.cnblogs.com/iwyc/p/15369163.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有