public List<Integer> dfs(TreeNode root)
{
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root == null)
return list;
stack.push(root);
while(!stack.isEmpty())
{
TreeNode t = stack.pop();
if(t.right != null)
stack.push(t.right);
if(t.left != null)
stack.push(t.left);
list.add(t.val);
}
return list;
}
数据结构--二叉树深度优先遍历
最新推荐文章于 2021-12-18 15:59:21 发布
1523

被折叠的 条评论
为什么被折叠?



