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

二叉树的后序遍历

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

using System;
using System.Collections.Generic;

/*
public class TreeNode
{
	public int val;
	public TreeNode left;
	public TreeNode right;

	public TreeNode (int x)
	{
		val = x;
	}
}
*/

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public List<int> postorderTraversal (TreeNode root) {
        List<int> result = new List<int>();
        Stack<TreeNode> st = new Stack<TreeNode>();
        TreeNode cur;
        if(root != null)st.Push(root);
        while(st.Count != 0){
            cur = st.Peek();
            if(cur != null){
                st.Pop();
                st.Push(cur);
                st.Push(null);
                if(cur.right != null) st.Push(cur.right);
                if(cur.left != null) st.Push(cur.left);
            }
            else{
                st.Pop();
                result.Add(st.Peek().val);
                st.Pop();
            }
        }
        return result;
    }
}

全部评论

相关推荐

04-14 20:10
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
不懂!!!:感觉你的项目描述太简单了,建议使用star描述法优化提炼一下,就是使用什么技术或方案解决了什么问题,有什么效果或成果,例如:对axios进行了二次封装,实现了请求的统一管理、错误的集中处理以及接口调用的简化,显著提高了开发效率和代码维护性,使用canvas技术实现了路线绘制功能,通过定义路径绘制函数和动态更新机制,满足了简化的导航可视化需求,提升了用户体验。像什么是使用其他组件库,基本功能描述就最好不要写到项目成果里面去了,加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务