题解 | #二叉树的后序遍历#

二叉树的后序遍历

http://www.nowcoder.com/practice/1291064f4d5d4bdeaefbf0dd47d78541


/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    List<Integer> list = new ArrayList<>();
    public int[] postorderTraversal (TreeNode root) {
        // write code here
        TreeNode node = root;
        
        //常规非递归
//          Stack<TreeNode> stack = new Stack<>();
//          TreeNode pre = null;
//         while(node != null || !stack.isEmpty()){
//             while(node != null){
//                 stack.push(node);
//                 node = node.left;
//             }
//             node = stack.pop();
//             if(node.right == null || node.right == pre){
//                 list.add(node.val);
//                 pre = node;
//                 node = null;
//             }else{
//                 stack.push(node);
//                 node = node.right;
//             }
//         }
       //morris遍历
         while(node != null){
            if(node.left != null){
                TreeNode cur = node.left;
                while(cur.right != null && cur.right != node){
                    cur = cur.right;
                }
                if(cur.right != node){
                    cur.right = node;
                    node = node.left;
                }else{
                    cur.right = null;
                    postMorris(node.left);  
                    node = node.right;
                }
            } else{
                postMorris(node.left);
             node = node.right;
            }
             
        }
        postMorris(root);   
        int[] arrays = new int[list.size()];
        int i = 0;
        for(int l : list){
            arrays[i++] = l;
        }
        return arrays;       
    }
    public void postMorris(TreeNode root){
        TreeNode node = reverseList(root);
        TreeNode cur = node;
        while(cur != null){
            list.add(cur.val);
            cur = cur.right;
        }
        reverseList(node);
    }
    public TreeNode reverseList(TreeNode root){
        TreeNode cur = root;
        TreeNode pre = null;
        while(cur != null){
            TreeNode node = cur.right;
            cur.right = pre;
            pre = cur;
            cur = node;
        }
        return pre;
    }

 



}
全部评论

相关推荐

NBA球星伦纳德:jd是这样的,工作连拧螺丝都算不上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11136次浏览 95人参与
# 你的实习产出是真实的还是包装的? #
1966次浏览 42人参与
# 巨人网络春招 #
11380次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7655次浏览 43人参与
# 简历第一个项目做什么 #
31756次浏览 341人参与
# 重来一次,我还会选择这个专业吗 #
433569次浏览 3926人参与
# MiniMax求职进展汇总 #
24132次浏览 309人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187228次浏览 1122人参与
# 牛客AI文生图 #
21453次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152469次浏览 888人参与
# 研究所笔面经互助 #
118974次浏览 577人参与
# 简历中的项目经历要怎么写? #
310384次浏览 4219人参与
# AI时代,哪些岗位最容易被淘汰 #
63881次浏览 828人参与
# 面试紧张时你会有什么表现? #
30517次浏览 188人参与
# 你今年的平均薪资是多少? #
213153次浏览 1039人参与
# 你怎么看待AI面试 #
180172次浏览 1258人参与
# 高学历就一定能找到好工作吗? #
64339次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76551次浏览 374人参与
# 我的求职精神状态 #
448150次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363543次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160685次浏览 1112人参与
# 校招笔试 #
471261次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务