145. Binary Tree Postorder Traversal

网友投稿 792 2022-11-11

145. Binary Tree Postorder Traversal

145. Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes’ values.

For example: Given binary tree [1,null,2,3],

\

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private List resultList = new ArrayList(); public List postorderTraversal(TreeNode root) { postOrder(root); return resultList; } private void postOrder(TreeNode node) { if (node == null) { return; } if (node.left != null) { postOrder(node.left); } if (node.right != null) { postOrder(node.right); } resultList.add(node.val); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:135. Candy
下一篇:174. Dungeon Game
相关文章

 发表评论

暂时没有评论,来抢沙发吧~