331:验证二叉树的前序序列化

This commit is contained in:
huangge1199 2021-04-23 13:22:07 +08:00
parent 3539a0cc78
commit b2dd7b6dfc
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,72 @@
//序列化二叉树的一种方法是使用前序遍历当我们遇到一个非空节点时我们可以记录下这个节点的值如果它是一个空节点我们可以使用一个标记值记录例如 #
//
// _9_
// / \
// 3 2
// / \ / \
// 4 1 # 6
/// \ / \ / \
//# # # # # #
//
//
// 例如上面的二叉树可以被序列化为字符串 "9,3,4,#,#,1,#,#,2,#,6,#,#"其中 # 代表一个空节点
//
// 给定一串以逗号分隔的序列验证它是否是正确的二叉树的前序序列化编写一个在不重构树的条件下的可行算法
//
// 每个以逗号分隔的字符或为一个整数或为一个表示 null 指针的 '#'
//
// 你可以认为输入格式总是有效的例如它永远不会包含两个连续的逗号比如 "1,,3"
//
// 示例 1:
//
// 输入: "9,3,4,#,#,1,#,#,2,#,6,#,#"
//输出: true
//
// 示例 2:
//
// 输入: "1,#"
//输出: false
//
//
// 示例 3:
//
// 输入: "9,#,#,1"
//输出: false
// Related Topics
// 👍 310 👎 0
package leetcode.editor.cn;
//331:验证二叉树的前序序列化
public class VerifyPreorderSerializationOfABinaryTree {
public static void main(String[] args) {
//测试代码
Solution solution = new VerifyPreorderSerializationOfABinaryTree().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isValidSerialization(String preorder) {
if (preorder == null || "#".equals(preorder)) {
return true;
}
String[] strs = preorder.split(",");
int num = 1;
for (int i = 0; i < strs.length; i++) {
String str = strs[i];
if (num == 1 && "#".equals(str) && i != strs.length - 1) {
return false;
}
if ("#".equals(str)) {
num--;
} else {
num++;
}
}
return num == 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,35 @@
<p>序列化二叉树的一种方法是使用前序遍历。当我们遇到一个非空节点时,我们可以记录下这个节点的值。如果它是一个空节点,我们可以使用一个标记值记录,例如 <code>#</code></p>
<pre> _9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
</pre>
<p>例如,上面的二叉树可以被序列化为字符串 <code>&quot;9,3,4,#,#,1,#,#,2,#,6,#,#&quot;</code>,其中 <code>#</code> 代表一个空节点。</p>
<p>给定一串以逗号分隔的序列,验证它是否是正确的二叉树的前序序列化。编写一个在不重构树的条件下的可行算法。</p>
<p>每个以逗号分隔的字符或为一个整数或为一个表示 <code>null</code> 指针的 <code>&#39;#&#39;</code></p>
<p>你可以认为输入格式总是有效的,例如它永远不会包含两个连续的逗号,比如&nbsp;<code>&quot;1,,3&quot;</code></p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入: </strong><code>&quot;9,3,4,#,#,1,#,#,2,#,6,#,#&quot;</code>
<strong>输出: </strong><code>true</code></pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入: </strong><code>&quot;1,#&quot;</code>
<strong>输出: </strong><code>false</code>
</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入: </strong><code>&quot;9,#,#,1&quot;</code>
<strong>输出: </strong><code>false</code></pre>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 310</li><li>👎 0</li></div>