zoukankan      html  css  js  c++  java
  • 94. Binary Tree Inorder Traversal

    /**
    * Binary Tree Inorder Traversal
    * @param {TreeNode} root
    * @return {number[]}
    * Inorder: left->root->right
    */
    var inorderTraversal = function (root) {  
      let stack = [], result = [];
      while (stack.length > 0 || root != null) {
        if (root != null) {
          stack.push(root);
          root = root.left;//
        } else {
          root = stack.pop();//pop方法用于删除并返回数组的最后一个元素。
          result.push(root);//只有inOrder才在这里push
          root = root.right;
        }
      }
    }
     
    //Kotlin
    class Solution {
        fun inorderTraversal(root_: TreeNode?): List<Int> {
             val result = arrayListOf<Int>()//read,write
            var root = root_
            val stack = Stack<TreeNode>()
            while (root != null || stack.size > 0) {
              if (root != null) {
                    stack.push(root)
                    root = root.left
                } else {
                    root = stack.pop()
                    result.add(root.`val`)
                    root = root.right
                }
            }
            return result
        }
    }
  • 相关阅读:
    CSS同时选择器
    create-react-app之proxy
    mysql limit语句
    给tbody加垂直滚动条的具体思路
    MySql数据类型范围
    block、inline、inline-block
    javascript sourcemap
    session of express
    React中innerHTML的坑
    box-sizing
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/10204331.html
Copyright © 2011-2022 走看看