• LeetCode-94 Binary Tree Inorder Traversal Solution (with Java)


    1. Description: 

    Notes: 

    2. Examples: 

    3.Solutions:

     1 /**
     2  * Created by sheepcore on 2019-05-09
     3  * Definition for a binary tree node.
     4  * public class TreeNode {
     5  *     int val;
     6  *     TreeNode left;
     7  *     TreeNode right;
     8  *     TreeNode(int x) { val = x; }
     9  * }
    10  */
    11 class Solution {
    12     public List<Integer> inorderTraversal(TreeNode root) {
    13         Stack<TreeNode> stack = new Stack<TreeNode>();
    14         List<Integer> res = new ArrayList<>();
    15         while (root != null || !stack.isEmpty()){
    16             while (root != null) {
    17                 stack.push(root);
    18                 root = root.left;
    19             }
    20             root = stack.pop();
    21             res.add(root.val);
    22             root = root.right;
    23         }
    24         return res;
    25     }
    26 }

     

  • 相关阅读:
    了解JVM原理
    封装JS
    “==”和Equals的区别
    SpringMVC请求RequestMapping() 请求乱码
    博客25周
    博客24周
    博客23周
    博客22周
    博客第21周
    博客第21周
  • 原文地址:https://www.cnblogs.com/sheepcore/p/12395506.html
Copyright © 2020-2023  润新知