Created
May 29, 2018 21:35
-
-
Save jianminchen/e4fca0040d091be836cf0df1b0c1b9ff to your computer and use it in GitHub Desktop.
Leetcode 94: binary tree inorder traversal - recursive solution, pass online judge.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Leetcode94_BinaryTreeInOrderTraversal | |
{ | |
/// <summary> | |
/// Leetcode 94: Binary tree inorder traversal | |
/// https://leetcode.com/problems/binary-tree-inorder-traversal/description/ | |
/// </summary> | |
class Program | |
{ | |
public class TreeNode { | |
public int val; | |
public TreeNode left; | |
public TreeNode right; | |
public TreeNode(int x) | |
{ | |
val = x; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
} | |
public IList<int> InorderTraversal(TreeNode root) // emtpy tree, with one root node, with more nodes | |
{ | |
if (root == null) | |
{ | |
return new List<int>(); | |
} | |
var left = InorderTraversal(root.left); | |
left.Add(root.val); | |
var right = InorderTraversal(root.right); | |
// add right to left list | |
if (right.Count > 0) | |
{ | |
foreach (var item in right) | |
{ | |
left.Add(item); | |
} | |
} | |
return left; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment