G面经Prepare: Valid Preorder traversal serialized String

1 求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如
2 A) 9 3 4 # # 1 # # 2 # 6 # #是对的,因为表示
3            9
4          /   5        3      2
6       /  \      7     4    1      6
8 B ) 9 3 4 # # 1 # #就是错的,因为无法反构造回一棵树

我觉得可以在字符串里找"n##"这种结构(对应tree里两个children都是Null的叶节点),找到之后就把"n##"改写成"#",也就是把找到的那个末端的子树想想成null,最后字符串变成"#"的就是valid,否则就invalid
比如 A) 9 3 "4 # #" "1 # #" 2 # "6 # #" ---> 9 3 # # 2 # # ---> 9 "3 # #" "2 # #" ---> 9 # # ---> "9 # #" ---> "#"
       B) 9 3 "4 # #" "1 # #" ---> 9 3 # # ---> 9 "3 # #" ---> 9 # (没有"n##"结构了,return false)

也可用stack来这样做,当前如果是#,stack peek如果也是#且size>=2,就pop两次,且再check当前这个#

 1 package PreorderValidSerialized;
 2 import java.util.*;
 3
 4 public class Solution {
 5     public boolean check(String str) {
 6         String[] strs = str.split(" ");
 7         Stack<String> stack = new Stack<String>();
 8         for (int i=0; i<strs.length; i++) {
 9             if (stack.size()>=2 && strs[i].equals("#") && stack.peek().equals("#")) {
10                 stack.pop();
11                 stack.pop();
12                 i--;
13             }
14             else stack.push(strs[i]);
15         }
16         if (stack.size()==1 && stack.peek().equals("#")) return true;
17         else return false;
18     }
19
20     /**
21      * @param args
22      */
23     public static void main(String[] args) {
24         // TODO Auto-generated method stub
25         Solution sol = new Solution();
26         boolean res = sol.check("9 3 4 # # 1 # # 2 # 6 # # #");
27         if (res) System.out.println("true");
28         else System.out.println("false");
29     }
30
31 }
时间: 2024-08-06 07:54:17

G面经Prepare: Valid Preorder traversal serialized String的相关文章

Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合

给定一个二叉树,以集合方式返回其中序/先序方式遍历的所有元素. 有两种方法,一种是经典的中序/先序方式的经典递归方式,另一种可以结合栈来实现非递归 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]. OJ's Binary Tree Serialization: The ser

Leetcode-Binary Tree Preorder Traversal

题目链接:https://oj.leetcode.com/problems/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,

Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / 2 3 / 4 5 return [1,2,4,5,3]. Thinking: For this problem, you need to think about using recursive or non-recursive methods. As recursive method, we should think the o

[LeetCode][JavaScript]Binary Tree Preorder Traversal

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? https://leetcod

144.Binary Tree Preorder Traversal(非递归前序遍历)

Given a binary tree, return the preorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution istrivial, could you do it iteratively? HideTags Tree Stack #pragma once #include<ios

[C++]LeetCode: 95 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? Answer 1: 递归法 思路:如果当前节点不为空,先把当前节点的值放入数组.从

LeetCode: Binary Tree Preorder Traversal 解题报告

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    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

144. Binary Tree Preorder Traversal

Problem Statement 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? solution one: traverse: 1 /*