Leetcode -- Day 65

Binary Tree

Question 1

Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes‘ values.

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

   1
         2
    /
   3

return [1,2,3].

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

 1 /**
 2  * Definition for a binary tree node.
 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     List<Integer> result = new ArrayList<Integer>();
12     public List<Integer> preorderTraversal(TreeNode root) {
13         preorderTraversal2(root);
14         return result;
15     }
16
17     public void preorderTraversal2(TreeNode root){
18         if (root == null)
19             return;
20         result.add(root.val);
21         preorderTraversal(root.left);
22         preorderTraversal(root.right);
23     }
24 }
时间: 2024-08-27 14:44:40

Leetcode -- Day 65的相关文章

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen

leetcode ||65、 Valid Number

problem: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be a

LeetCode (65):Same tree

Total Accepted: 83663 Total Submissions: 200541 Difficulty: Easy Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 首

Leetcode 65. Valid Number 验证数字 解题报告

1 解题思想 更新下:这道突然就很多访问,想起来好像是lt提交通过率最低的,嗯,我写的也不是特别详细,如有问题可以新浪微博@MebiuW交流~~ 这道题条条框框是在太多了,各种情况..不过简略来说,正确的做法应该是: 1.熟悉数字的表述规则(可以看网上的,也可以看我代码的),这道题关键是要懂所有的数字规则. 2.对输入的数字首先进行必要的检测,是否有abc或者中间空格等非法字符 3.将e前面和e后面分开计算!e前面允许有小数点形式的,e后面不允许有小数点形式的 4.数字的形式一般是 可以有正负号

[LeetCode]65. Factorial Trailing Zeros阶乘的尾零个数

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. Subscribe to see which companies asked th

leetCode 65.Valid Number (有效数字)

Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to

笔试题65. LeetCode OJ (52)

这个题的意思是让我们求"n 后问题"的解法的数目,其实和上一个题基本是相似的,只需要做少量的改动就行,主要的思路如下: 1.使用 vector<string> 保存每一层的Queen的位置,然后往下一层递归,测试下一层Queen的位置,若符合要求就继续上下测试,若不符合则更换位置继续测试. 2.测试函数的编写需要注意,记得斜线上的点的测试,假设我们先将Queen置于中心上,那么该点的左右,上下,以及左上方和右下方,左下方和右上方等等都需要考虑到,不要漏掉任何一点,而且需要考

[LeetCode 65] Valid Number (通过率最低的一题)

题目链接:valid-number /** * Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem st

[LeetCode] 65. Valid Number Java

题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true Note: It is intended for the problem statement to be ambiguous.