/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ls=new ArrayList<Integer>();
if(root == null)
{return ls;}
Stack<TreeNode> st=new Stack<TreeNode>();
HashSet<TreeNode> hs = new HashSet<TreeNode>();
st.push(root);
while(!st.isEmpty())
{
TreeNode temp=st.pop();
if(hs.contains(temp))
{
ls.add(temp.val);
continue;
}
hs.add(temp);
if(temp.right!=null)
{
st.push(temp.right);
}
st.push(temp);
if(temp.left!=null)
{
st.push(temp.left);
}
}
return ls;
}
}
二叉树中序遍历非递归
最新推荐文章于 2022-06-29 16:56:47 发布