zoukankan      html  css  js  c++  java
  • leetcode------Binary Tree Inorder Traversal

    标题: Binary Tree Inorder Traversal
    通过率: 36.1%
    难度: 中等

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

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3
    

    return [1,3,2].

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

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

    二叉树的中序遍历,直接看代码:

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public List<Integer> inorderTraversal(TreeNode root) {
    12         ArrayList<Integer> result=new ArrayList<Integer>();
    13         vistTree(result,root);
    14         return result;
    15     }
    16     public void vistTree(List<Integer> temp,TreeNode root){
    17         if(root!=null){
    18             vistTree(temp,root.left);
    19             temp.add(root.val);
    20             vistTree(temp,root.right);
    21         }
    22     }
    23 }
  • 相关阅读:
    pop指令的执行过程
    ipad 4.2 开发环境
    XP远程桌面
    停机问题、哥德尔定理
    看MIT的线性代数
    VizA项目继续推进
    port systemc to mac os x
    关于jar
    普桑
    拉突爾
  • 原文地址:https://www.cnblogs.com/pkuYang/p/4311400.html
Copyright © 2011-2022 走看看