当前位置:网站首页>Force buckle 145 Binary Tree Postorder Traversal

Force buckle 145 Binary Tree Postorder Traversal

2022-07-07 07:55:00 Yangshiwei....

subject :

analysis :

After the sequence traversal , First left node , Rear right node , Finally, its own node , The difference between the first, middle and last traversals is the difference in order , Just exchange the code and position

Code :

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list =new ArrayList();
        last(root,list);
        return list;
    }
    public void last(TreeNode node,List<Integer> list){
        if(node!=null){
            if(node.left!=null){
                last(node.left,list);
            }if(node.right!=null){
                last(node.right,list);
            }
            list.add(node.val);
        }
    }
}

原网站

版权声明
本文为[Yangshiwei....]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130647529279.html