博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
94. Binary Tree Inorder Traversal 二叉树中序遍历
阅读量:4660 次
发布时间:2019-06-09

本文共 791 字,大约阅读时间需要 2 分钟。

Given a binary tree, return the inorder traversal of its nodes' values.

For example:

Given binary tree [1,null,2,3],

1    \     2    /   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

题意:不使用递归的方式,中序遍历二叉树

 
  1. /**
  2. * Definition for a binary tree node.
  3. * function TreeNode(val) {
  4. * this.val = val;
  5. * this.left = this.right = null;
  6. * }
  7. */
  8. /**
  9. * @param {TreeNode} root
  10. * @return {number[]}
  11. */
  12. let inorderTraversal = (root) => {
  13. let res = [];
  14. if (root == null) return res;
  15. let stack = [];
  16. var node = root;
  17. while (node != null || stack.length != 0) {
  18. while (node != null) {
  19. stack.push(node);
  20. node = node.left;
  21. }
  22. if (stack.length != 0) {
  23. node = stack.pop();
  24. res.push(node.val)
  25. node = node.right;
  26. }
  27. }
  28. return res;
  29. }

转载于:https://www.cnblogs.com/xiejunzhao/p/8127976.html

你可能感兴趣的文章
Easy-ARM IMX283 移植RTL8192CU驱动
查看>>
javascript-装饰者模式
查看>>
最近的几个任务
查看>>
去哪儿网2015校园招聘产品经理笔试(时间:2014-9-23)
查看>>
java默认继承
查看>>
关闭 禁用 Redis危险命令
查看>>
三年工作总结
查看>>
MySQL数据库实验:任务二 表数据的插入、修改及删除
查看>>
asp.net网站前台通过DataList展示信息的代码
查看>>
【SAS ADVANCE】Performing Queries Using PROC SQL
查看>>
Hive新功能 Cube, Rollup介绍
查看>>
webpack:(模块打包机)
查看>>
程序员不得不知的座右铭(世界篇)
查看>>
表格-鼠标经过单元格变色(暂不支持IE6)
查看>>
【每日一学】pandas_透视表函数&交叉表函数
查看>>
实时读取日志文件
查看>>
【寒假集训系列2.12】
查看>>
2018牛客多校第六场 I.Team Rocket
查看>>
Vuex了解
查看>>
c++初始化函数列表
查看>>