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

二叉树的后序遍历

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

import java.util.*;
public class Solution {
    public int[] postorderTraversal (TreeNode root) {
        // write code here
        if(root==null)return new int[0];
        List<Integer> list = new ArrayList<>();
        postOr(list,root);
        int[] res=new int[list.size()];
        for(int i=0;i<res.length;i++){
            res[i]=list.get(i);
        }
        return res;
    }
    
  //递归方法
    public void postOr(List<Integer> list,TreeNode t){
        if(t.left!=null) postOr(list,t.left);
        if(t.right!=null) postOr(list,t.right);
        list.add(t.val);
    }
}

递归遍历树

按题意所说,通过递归遍历树来获得其后续遍历

每次先遍历左子树,再右子树,最后根

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-20 14:14
点赞 评论 收藏
分享
05-12 17:28
已编辑
门头沟学院 硬件开发
ldf李鑫:不说公司名祝你以后天天遇到这样的公司
点赞 评论 收藏
分享
兄弟们你们进大厂靠的是什么项目啊
DOTPHTP:课设改。其实项目什么的如果不是实习里面的生产项目的话,建议✍️那种自己想要做的。突出个人自驱力,而不是为了找工作不得不随波逐流这种
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务