mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-27 12:22:45 +08:00
Reformat the C# codes.
Disable creating new line before open brace.
This commit is contained in:
@ -6,25 +6,19 @@
|
||||
|
||||
namespace hello_algo.include;
|
||||
|
||||
public class TreeNode
|
||||
{
|
||||
/* 二叉树节点类 */
|
||||
public class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点引用
|
||||
public TreeNode? right; // 右子节点引用
|
||||
|
||||
public TreeNode(int x)
|
||||
{
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree given an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? ListToTree(List<int?> arr)
|
||||
{
|
||||
/* Generate a binary tree given an array */
|
||||
public static TreeNode? ListToTree(List<int?> arr) {
|
||||
if (arr.Count == 0 || arr[0] == null)
|
||||
return null;
|
||||
|
||||
@ -32,18 +26,15 @@ public class TreeNode
|
||||
Queue<TreeNode> queue = new Queue<TreeNode>();
|
||||
queue.Enqueue(root);
|
||||
int i = 0;
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
while (queue.Count != 0) {
|
||||
TreeNode node = queue.Dequeue();
|
||||
if (++i >= arr.Count) break;
|
||||
if (arr[i] != null)
|
||||
{
|
||||
if (arr[i] != null) {
|
||||
node.left = new TreeNode((int)arr[i]);
|
||||
queue.Enqueue(node.left);
|
||||
}
|
||||
if (++i >= arr.Count) break;
|
||||
if (arr[i] != null)
|
||||
{
|
||||
if (arr[i] != null) {
|
||||
node.right = new TreeNode((int)arr[i]);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
@ -51,41 +42,26 @@ public class TreeNode
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public static List<int?> TreeToList(TreeNode root)
|
||||
{
|
||||
/* Serialize a binary tree to a list */
|
||||
public static List<int?> TreeToList(TreeNode root) {
|
||||
List<int?> list = new();
|
||||
if (root == null) return list;
|
||||
Queue<TreeNode?> queue = new();
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
while (queue.Count != 0) {
|
||||
TreeNode? node = queue.Dequeue();
|
||||
if (node != null)
|
||||
{
|
||||
if (node != null) {
|
||||
list.Add(node.val);
|
||||
queue.Enqueue(node.left);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
list.Add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tree node with specific value in a binary tree
|
||||
* @param root
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? GetTreeNode(TreeNode? root, int val)
|
||||
{
|
||||
/* Get a tree node with specific value in a binary tree */
|
||||
public static TreeNode? GetTreeNode(TreeNode? root, int val) {
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
|
Reference in New Issue
Block a user