非递归遍历二叉树
来源:互联网 发布:mac出现文件夹问号 编辑:程序博客网 时间:2024/06/05 18:20
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
题目连接:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list= new ArrayList<Integer>(); Stack<TreeNode> stack =new Stack<TreeNode>(); if(root==null) return list; stack.push(root); while(!stack.isEmpty()){ TreeNode tree=stack.pop(); list.add(tree.val); if(tree.right!=null){ stack.push(tree.right); } if(tree.left!=null){ stack.push(tree.left); } } return list; }}
0 0
- 非递归遍历二叉树
- 二叉树遍历非递归
- 非递归遍历二叉树
- 二叉树非递归遍历
- 二叉树非递归遍历
- 非递归遍历二叉树
- 二叉树非递归遍历
- 非递归遍历二叉树
- 非递归遍历二叉树
- 非递归遍历二叉树
- 非递归遍历二叉树
- 非递归遍历二叉树
- 二叉树非递归遍历
- 二叉树遍历--非递归
- 二叉树非递归遍历
- 非递归遍历二叉树
- 二叉树非递归遍历
- 二叉树非递归遍历
- Error creating bean with name 。。。
- Phoenix——实现向HBase发送标准SQL语句
- C++学习计划
- 软考笔记--项目进度管理
- 软考笔记--项目成本管理
- 非递归遍历二叉树
- bzoj1600 [Usaco2008 Oct]建造栅栏
- HDOJ-1253(BFS + 特判)
- Ubuntu 用 apache2 搭建 web 服务器 建wordpress博客
- Handling bundles in activities and fragments
- java知识(1)
- 怎样把文件放在图片中
- 设计模式之抽象工厂模式
- CentOS 搭建svn服务器