标签:push 读取 迭代 null pop lis 代码 root treenode
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3] 1 2 / 3 输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public IList<int> PostorderTraversal(TreeNode root) {
var list = new List<int>();
if (root == null) return list;
TreeNode pre = null;
var stack = new Stack<TreeNode>();
stack.Push(root);
/*当前节点被读取的条件为:无左右孩子,或者上一次读取的为其左孩子且右孩子为空,或者上一次读取的为其右孩子。*/
while (stack.Count > 0)
{
TreeNode node = stack.Peek();
if((node.left == null && node.right ==null)
|| (pre != null && (pre == node.right || (pre == node.left && node.right == null)))){
list.Add(node.val);
stack.Pop();
pre = node;
}
else{
if(node.right != null) stack.Push(node.right);
if(node.left != null) stack.Push(node.left);
}
}
return list;
}
}
标签:push 读取 迭代 null pop lis 代码 root treenode
原文地址:https://www.cnblogs.com/fuxuyang/p/14242728.html