zoukankan      html  css  js  c++  java
  • 中序遍历

    package cn.jiedada.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;
    
    /**
     * 数的中序遍历
     */
    class Solution {
        public List< Integer > inorderTraversal(TreeNode root) {
            //定义两个参数
            List < Integer > res = new ArrayList< >();
            Stack< TreeNode > stack = new Stack < > ();
            //如果节点不为null或者stack里面还有节点就进行循环
            while ( root!=null || stack != null){
                //获得所有坐儿子入stack
                while (root!=null){
                    stack.push(root);
                    root=root.left;
                }
                //当没有左儿子的时候就需要出stack
                root = stack.pop();
                res.add(root.val);
                //最后走右儿子
                root = root.right;
            }
            return res;
        }
    }

    力扣连接 https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/zhuan-ti-jiang-jie-er-cha-shu-qian-zhong-hou-xu--2/

  • 相关阅读:
    5.Longest Palindrome substring
    3. Longest Substring Without Repeating Characters
    1.Two Sum
    2.Add two Numbers
    oplog
    airflow笔记
    airflow
    grpc protobuf
    modbus
    Linux 工具,一本好书 大牛的博客
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/15033106.html
Copyright © 2011-2022 走看看