62 lines
1.5 KiB
Java
62 lines
1.5 KiB
Java
//给定一个二叉树,返回它的 后序 遍历。
|
|
//
|
|
// 示例:
|
|
//
|
|
// 输入: [1,null,2,3]
|
|
// 1
|
|
// \
|
|
// 2
|
|
// /
|
|
// 3
|
|
//
|
|
//输出: [3,2,1]
|
|
//
|
|
// 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
|
|
// Related Topics 栈 树
|
|
// 👍 564 👎 0
|
|
|
|
package leetcode.editor.cn;
|
|
|
|
import com.code.leet.entiy.TreeNode;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
//145:二叉树的后序遍历
|
|
public class BinaryTreePostorderTraversal{
|
|
public static void main(String[] args) {
|
|
//测试代码
|
|
Solution solution = new BinaryTreePostorderTraversal().new Solution();
|
|
}
|
|
//力扣代码
|
|
//leetcode submit region begin(Prohibit modification and deletion)
|
|
/**
|
|
* 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<>();
|
|
if(root==null){
|
|
return list;
|
|
}
|
|
list.addAll(postorderTraversal(root.left));
|
|
list.addAll(postorderTraversal(root.right));
|
|
list.add(root.val);
|
|
return list;
|
|
}
|
|
}
|
|
//leetcode submit region end(Prohibit modification and deletion)
|
|
|
|
} |